package com.baidu.cmsService.services.impls;

import com.baidu.cmsCommes.pojo.*;
import com.baidu.cmsCommes.util.MD5Util;
import com.baidu.cmsService.dao.*;
import com.baidu.cmsService.services.UserService;
import com.sun.org.apache.bcel.internal.generic.I2F;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Null;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: BSS
 * @create: 2020/6/17/20:30
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;

    @Resource
    private UserGroupDao userGroupDao;

    @Resource
    private UserRoleDao userRoleDao;

    @Resource
    private GroupChannelDao groupChannelDao;

    @Resource
    private ChannelDao channelDao;

    @Resource
    private RoleDao roleDao;


    public Pager<User> userList() {
        List<User> users = userDao.userList();
        Integer total = userDao.countAll();
        Pager<User> pager = new Pager<User>(10,1,total,users);
        return pager;
    }

    public Boolean updateStatusById(Integer id) {
        User user = userDao.selectUserById(id);

        if (user==null)
            return false;

        Integer status = user.getStatus();
        user.setStatus(status==1?0:1);

        Integer isSuccess = userDao.updateUser(user);

        if (isSuccess==0)
            return false;

        return true;
    }

    public Boolean deleteUser(Integer id) {
        return userDao.deleteUser(id)==1?true:false;
    }

    public UserDto selectUserById(Integer id) {

        User user = userDao.selectUserById(id);

        if (user==null)
            return null;

        UserDto userDto = new UserDto(user);

        List<UserRole> userRoles = userRoleDao.selectByUid(user.getId());
        List<UserGroup> userGroups = userGroupDao.selectByUid(user.getId());

        if (userRoles==null || userGroups==null)
            return null;

        Integer[] userRolesArr = new Integer[userRoles.size()];
        Integer[] userGroupsArr = new Integer[userGroups.size()];

        for (int i = 0; i < userRoles.size(); i++) {
            userRolesArr[i] = userRoles.get(i).getR_id();
        }

        for (int i = 0; i < userGroups.size(); i++) {
            userGroupsArr[i] = userGroups.get(i).getG_id();
        }

        userDto.setRoleIds(userRolesArr);
        userDto.setGroupIds(userGroupsArr);

        return userDto;
    }

    public Boolean saveUser(UserDto userDto) {
        User user = userDto.getUser();

        user.setCreate_date(new Date());

        user.setPassword(MD5Util.GetMD5Code(user.getPassword()));

        Integer[] roleIds = userDto.getRoleIds();
        Integer[] groupIds = userDto.getGroupIds();

        Integer roleSave = 0;
        Integer groupSave = 0;

        Integer integer = userDao.saveUser(user);

        for (Integer groupId : groupIds) {
            groupSave += userGroupDao.save(null,groupId,user.getId());
        }

        for (Integer roleId : roleIds) {
             roleSave += userRoleDao.save(null, roleId, user.getId());
        }

        Integer totalSave = roleIds.length+groupIds.length;

        return totalSave==(roleSave+groupSave) && integer!=null?true:false;
    }

    public Boolean updateUser(UserDto userDto) {
        Integer uid = userDto.getId();
        User user = userDao.selectUserById(uid);

        if (user==null)
            return false;

        User userDate = userDto.getUser();
        userDate.setCreate_date(user.getCreate_date());
        userDate.setPassword(MD5Util.GetMD5Code(userDate.getPassword()));

        //保存user
        userDao.updateUser(userDate);

        //删除user下的roles
        userRoleDao.deleteRoleByUid(uid);

        //删除user下的groups
        userGroupDao.deleteGroupByUid(uid);

        //重新添加roles  groups
        Integer[] roleIds = userDto.getRoleIds();
        Integer[] groupIds = userDto.getGroupIds();

        /*Integer roleSave = 0;
        Integer groupSave = 0;*/

        for (Integer groupId : groupIds) {
            userGroupDao.save(null,groupId,user.getId());
        }

        for (Integer roleId : roleIds) {
            userRoleDao.save(null, roleId, user.getId());
        }

        return true;
    }

    public List<Channel> listChannels(Integer id) {
        //根据用户id查询出分组id
        List<UserGroup> userGroups = userGroupDao.selectByUid(id);
        //再根据分组id查询channel
        List<GroupChannel> list = new ArrayList<GroupChannel>();
        for (UserGroup userGroup : userGroups) {
            Integer gid = userGroup.getG_id();
            List<GroupChannel> groupChannelsList = groupChannelDao.selectByGid(gid);
            list.addAll(groupChannelsList);
        }

        List<Channel> channelList = new ArrayList<Channel>();

        for (GroupChannel groupChannel : list) {
            Channel channel = channelDao.selectById(groupChannel.getC_id());
            channelList.add(channel);
        }

        return channelList;
    }

    public Boolean isAdmin(Integer id) {
        List<UserRole> userRoles = userRoleDao.selectByUid(id);
        List<Role> roleList = new ArrayList<Role>();

        for (UserRole userRole : userRoles) {
            roleList.add(roleDao.selectById(userRole.getR_id()));
        }

        if (roleList.size()==0)
            return false;

        for (Role role : roleList) {
            if (role.getRoleType().equals(RoleType.ROLE_ADMIN))
                return true;
        }

        return false;
    }


}
